﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE80;
using EnvDTE;

namespace dFactor2.CodeHelpers
{
    public class Project : CodeHelperBase
    {
        private EnvDTE.Window _slnExplorer;
        UIHierarchy _slnHierarchy;
        UIHierarchyItem _prjSelection;

        public Project(DTE2 applicationObject)
            : base(applicationObject)
        {
            _slnExplorer = ApplicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer);
            _slnHierarchy = (UIHierarchy)_slnExplorer.Object;
            _slnExplorer.Activate();

            _prjSelection = (UIHierarchyItem)((Object[])_slnHierarchy.SelectedItems)[0];

        }

        public void CollapseProjectItems()
        {
            foreach (UIHierarchyItem ProjectItem in _prjSelection.UIHierarchyItems)
            {
                ChangeItemTreeMode(ProjectItem, false);
            }
        }

        public void ExpandProjectItems()
        {
            foreach (UIHierarchyItem ProjectItem in _prjSelection.UIHierarchyItems)
            {
                ChangeItemTreeMode(ProjectItem, true);
            }

        }

        public void CollapseProject()
        {
            _prjSelection.UIHierarchyItems.Expanded = false;
            foreach (UIHierarchyItem ProjectItem in _prjSelection.UIHierarchyItems)
            {
                ChangeItemTreeMode(ProjectItem, false);
            }
        }

        public void ExpandProject()
        {
            _prjSelection.UIHierarchyItems.Expanded = true;

            foreach (UIHierarchyItem ProjectItem in _prjSelection.UIHierarchyItems)
            {
                ChangeItemTreeMode(ProjectItem, true);
            }
            
        }

        private void ChangeItemTreeMode(UIHierarchyItem item, bool expanded)
        {
            item.UIHierarchyItems.Expanded = expanded;

            if (item.UIHierarchyItems.Count > 0)
            {
                foreach (UIHierarchyItem childItem in item.UIHierarchyItems)
                {
                    ChangeItemTreeMode(childItem, expanded);
                }
            }
        }

    }
}
